package com.accesshollywood.grfxOrdering.basics;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;

import com.accesshollywood.SQL.GFXSQL;
import com.accesshollywood.indexables.GraphicType;

public class GraphicTypes {
	public static HashMap<Integer, GraphicType> allGraphicTypes;
	public static HashMap<Integer, GraphicType> AHGraphicTypes;
	public static HashMap<Integer, GraphicType> LIVEGraphicTypes;
	public static DefaultComboBoxModel graphicTypesModel;
	public static DefaultListModel AHGraphicTypesModel;
	public static DefaultListModel LIVEGraphicTypesModel;

	public static void populateGraphicTypes() {
		allGraphicTypes = new HashMap<Integer, GraphicType>();
		AHGraphicTypes = new HashMap<Integer, GraphicType>();
		LIVEGraphicTypes = new HashMap<Integer, GraphicType>();
		graphicTypesModel = new DefaultComboBoxModel();
		AHGraphicTypesModel = new DefaultListModel();
		LIVEGraphicTypesModel = new DefaultListModel();

		try {
			ResultSet graphicTypeResults = GFXSQL.getResultSet("graphicType", "*", "");
			while (graphicTypeResults.next()) {
				GraphicType currGraphicType = new GraphicType();
				currGraphicType.fromSQL(graphicTypeResults.getInt("idGraphicType"));
//				currGraphicType.idGraphicType = graphicTypeResults.getInt("idGraphicType");
//				currGraphicType.name = graphicTypeResults.getString("name");
//				currGraphicType.prefix = graphicTypeResults.getString("prefix");
//				currGraphicType.idShowName = graphicTypeResults.getInt("idShowName");
				addGraphicTypeToLists(currGraphicType);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void addGraphicTypeToLists(GraphicType newGraphicType) {
		allGraphicTypes.put(newGraphicType.getId(), newGraphicType);
		if(newGraphicType.getShowName().getId() == 1){
			AHGraphicTypes.put(newGraphicType.getId(), newGraphicType);
			AHGraphicTypesModel.addElement(newGraphicType);
		}
		if(newGraphicType.getShowName().getId() == 2){
			LIVEGraphicTypes.put(newGraphicType.getId(), newGraphicType);
			LIVEGraphicTypesModel.addElement(newGraphicType);
		}
		if(graphicTypesModel.getSize() > 0){
			graphicTypesModel.insertElementAt(newGraphicType, graphicTypesModel.getSize()-1);
		}else{
			graphicTypesModel.insertElementAt(newGraphicType, graphicTypesModel.getSize());
		}
	}

	public static void addGraphicTypeToSQL(GraphicType newGraphicType) {
		GFXSQL.addGraphicType(newGraphicType);
	}
}
